
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class VelocityPass : VScriptableRenderPass
{
   
    
    
    internal RenderTextureDescriptor descriptor { get; set; }
    int velocityBuffer;
    Matrix4x4 previousGPUVPMatrix;
    Vector2 previousJitterOffset;
    public VelocityPass(RenderPassEvent renderPassEvent):base(renderPassEvent)
    {
        velocityBuffer = Shader.PropertyToID("_VelocityBuffer");
    }
    public  void Setup(ScriptableRenderContext context, ref VRenderingData renderingData)
    {
        var cmd = CommandBufferPool.Get();
        var camera = renderingData.cameraData.camera;
        renderingData.cameraData.camera.depthTextureMode |= DepthTextureMode.MotionVectors | DepthTextureMode.Depth;

        cmd.GetTemporaryRT(velocityBuffer, camera.pixelWidth, camera.pixelHeight, 32, FilterMode.Point, RenderTextureFormat.RGFloat);
        if (renderingData.FrameCount == 0)
            previousGPUVPMatrix = SaveGPUViewProjection(renderingData);
        

        context.ExecuteCommandBuffer(cmd);
        cmd.Clear();
        CommandBufferPool.Release(cmd);
    }
    public override void OnCameraSetup(CommandBuffer cmd, ref VRenderingData renderingData)
    {
       
       
    }
    public override void OnCameraCleanup(CommandBuffer cmd)
    {
        
        cmd.ReleaseTemporaryRT(velocityBuffer);
        
    }
    public override void Execute(ScriptableRenderContext context, ref VRenderingData renderingData)
    {
       
       
    }
    Matrix4x4 SaveGPUViewProjection(VRenderingData renderingData)
        => GL.GetGPUProjectionMatrix(renderingData.JitteredProjectionMatrix, false) * renderingData.ViewMatrix;
}